package com.xzy.controller;


import com.xzy.config.Audience;
import com.xzy.entity.Department;
import com.xzy.entity.Report;
import com.xzy.entity.User;
import com.xzy.entity.mReport;
import com.xzy.service.DepartmentService;
import com.xzy.service.UserService;
import com.xzy.service.mReportService;
import com.xzy.util.JSONResult;
import com.xzy.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
public class mReportController {
    @Autowired
    private mReportService mreportService;
    @Autowired
    private Audience audience;
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/zreport/mreport/addmReport")
    public JSONResult addmReport(@RequestBody mReport mreport, HttpServletRequest request)
    {
        String token = request.getHeader("Authorization");
        String _userId= JWTUtils.getUserId(token,audience.getBase64Secret());
        int userId=Integer.parseInt(_userId);
        User user=userService.getUser(userId);
        mreport.setUserId(userId);
        mreport.setUserName(user.getUserName());
        Department department=departmentService.getDepartmentByName(mreport.getDepartmentName());
        mreport.setDepartmentId(department.getDepartmentId());
        mreport.setStatus(false);
        StringBuilder sb=new StringBuilder();
        for(String str:mreport.getSummaryList())
        {
             sb.append(str+";");
        }
        String summaryContents=sb.toString();
        mreport.setSummaryContents(summaryContents);
        StringBuilder sb1=new StringBuilder();
        for(String str:mreport.getPlanList())
        {
            sb1.append(str+";");
        }
        String planContens=sb1.toString();
        mreport.setPlanContents(planContens);
        mreportService.addmReport(mreport);
        mReport mReport1=mreportService.getmReportByName(mreport.getMreportName(),mreport.getMreportDate());
        mreport.setMreportId(mReport1.getMreportId());
        String str1=mReport1.getSummaryContents();
        String []str1s=str1.split(";");
        mreport.setSummaryList(Arrays.asList((str1s)));
        return JSONResult.SUCCESS(mreport);

    }
    @GetMapping("/zreport/mreport/getAllmReport")
    public JSONResult getAllmReport()
    {
        List<mReport> mreports=mreportService.getAllmReport();
        for(mReport mreport:mreports)
        {
            User user=userService.getUser(mreport.getUserId());
            mreport.setUserName(user.getUserName());
            Department department=departmentService.getDepartment(mreport.getDepartmentId());
            mreport.setDepartmentName(department.getDepartmentName());
            String str1=mreport.getSummaryContents();
            String []str1s=str1.split(";");
            mreport.setSummaryList(Arrays.asList((str1s)));
            String str2=mreport.getPlanContents();
            String []str2s=str2.split(";");
            mreport.setPlanList(Arrays.asList(str2s));
        }
        return JSONResult.SUCCESS(mreports);
    }
    @DeleteMapping("/zreport/mreport/deletemReport/{mreportId}")
    public JSONResult deletemReport(@PathVariable("mreportId")int mreportId)
    {
        mreportService.deletemReport(mreportId);
        return JSONResult.SUCCESS();
    }
    @PutMapping("/zreport/mreport/updatemReport/{mreportId}")
    public JSONResult updatemReport(@RequestBody mReport mreport,@PathVariable("mreportId")int mreportId,HttpServletRequest request)
    {
        String token=request.getHeader("Authorization");
        String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
        int userId=Integer.parseInt(_userId);
        User user=userService.getUser(userId);
        mReport mreport1=mreportService.getmReport(mreportId);
        if(mreport1.getUserId()!=userId || !user.getUserRights())
        {
            return JSONResult.PERMISSION_UNAUTHORISE();
        }
        mreport.setUserName(user.getUserName());
        mreport.setMreportId(mreportId);
        Department department=departmentService.getDepartmentByName(mreport.getDepartmentName());
        int departmentId=department.getDepartmentId();
        mreport.setDepartmentId(departmentId);
        mreport.setUserId(userId);
        StringBuilder sb=new StringBuilder();
        for(String str:mreport.getSummaryList())
        {
            sb.append(str+";");
        }
        String summaryContents=sb.toString();
        mreport.setSummaryContents(summaryContents);
        StringBuilder sb1=new StringBuilder();
        for(String str:mreport.getPlanList())
        {
            sb1.append(str+";");
        }
        String planContens=sb1.toString();
        mreport.setPlanContents(planContens);
        mreportService.updatemReport(mreport);
        return JSONResult.SUCCESS(mreport);
    }
}
